package team.seekers.hula.message.tunnel.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.message.message.entity.dao.MessageDO;
import team.seekers.hula.message.message.service.MessageService;
import team.seekers.hula.message.tunnel.entity.dao.TunnelDO;
import team.seekers.hula.message.tunnel.entity.dto.TunnelMessageQueryDTO;
import team.seekers.hula.message.tunnel.entity.dto.TunnelPageQueryDTO;
import team.seekers.hula.message.tunnel.entity.vo.TunnelListVO;
import team.seekers.hula.message.tunnel.entity.vo.TunnelMessageListVO;
import team.seekers.hula.message.tunnel.mapper.TunnelMapper;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author ekin
 * @Description GOOD
 * @mood happy
 */
@Service
public class TunnelService extends ServiceImpl<TunnelMapper, TunnelDO> {


    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    public PageResult<TunnelListVO> pageTunnel(TunnelPageQueryDTO queryDTO) {
        LambdaQueryWrapper<TunnelDO> wrapper = new LambdaQueryWrapper<>();

        Optional.of(queryDTO).map(TunnelPageQueryDTO::getUserId).ifPresent(userId -> {
            wrapper.eq(TunnelDO::getUserId, userId);
        });
        Optional.of(queryDTO).map(TunnelPageQueryDTO::getAnotherId).ifPresent(anotherId -> {
            wrapper.eq(TunnelDO::getAnotherId, anotherId);
        });
        Optional.of(queryDTO).map(TunnelPageQueryDTO::getStatus).ifPresent(status -> {
            wrapper.eq(TunnelDO::getStatus, status);
        });
        wrapper.orderByDesc(TunnelDO::getGmtCreate);

        List<TunnelDO> tunnelDOS = list(wrapper);
        if (tunnelDOS.size() == 0) {
            return new PageResult<>();
        }

        List<TunnelListVO> list = new ArrayList<>();
        for (TunnelDO tunnelDO : tunnelDOS) {
            TunnelListVO tunnelListVO = new TunnelListVO();
            BeanUtils.copyProperties(tunnelDO, tunnelListVO);
            UserDO one = userService.getOne(new QueryWrapper<UserDO>().eq("id", tunnelDO.getUserId()));
            if(null != one){
                Optional.of(one).map(UserDO::getNickname).ifPresent(tunnelListVO::setUserName);
            }
            UserDO anotherOne = userService.getOne(new QueryWrapper<UserDO>().eq("id", tunnelDO.getAnotherId()));
            if(null != anotherOne){
                Optional.of(anotherOne).map(UserDO::getNickname).ifPresent(tunnelListVO::setAnotherName);
            }

            list.add(tunnelListVO);
        }

        Integer total = lambdaQuery().eq(TunnelDO::getUserId, queryDTO.getUserId()).count();
        return new PageResult<>(total, list);
    }

    public PageResult<TunnelMessageListVO> pageTunnelMessage(TunnelMessageQueryDTO queryDTO) {
        LambdaQueryWrapper<MessageDO> wrapper = new LambdaQueryWrapper<>();
        Optional.of(queryDTO).map(TunnelMessageQueryDTO::getTunnelId).ifPresent(tunnelId -> {
            wrapper.eq(MessageDO::getTunnelId, tunnelId);
        });
        wrapper.orderByDesc(MessageDO::getGmtCreate);

        List<MessageDO> messageDOS = messageService.list(wrapper);
        if (messageDOS.size() == 0) {
            return new PageResult<>();
        }

        List<TunnelMessageListVO> list = new ArrayList<>();
        for (MessageDO messageDO : messageDOS) {
            TunnelMessageListVO tunnelMessageListVO = new TunnelMessageListVO();
            BeanUtils.copyProperties(messageDO, tunnelMessageListVO);

            list.add(tunnelMessageListVO);
        }

        Integer total = messageService.lambdaQuery().eq(MessageDO::getTunnelId, queryDTO.getTunnelId()).count();
        return new PageResult<>(total, list);
    }
}
